﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace ExplodingCow.Basilisk.Logic
{
    /// <summary>
    /// Datatype that is simply used to maintain, load and save Items on the list
    /// </summary>
    public class ItemList
    {
        /// <summary>
        /// private static instance of ItemList
        /// </summary>
        private static ItemList instance;

        /// <summary>
        /// ItemList simply maintains a list of items, this is the list it uses
        /// </summary>
        List<Item> items;
        /// <summary>
        /// Get or Set the items in the list
        /// </summary>
        public List<Item> Items
        {
            get { return items; }
            //may remove the setter, depends on what we want to be able to do
            set { items = value; }
        }
        /// <summary>
        /// private constructor
        /// </summary>
        private ItemList()
        {
            items = new List<Item>();
        }
        /// <summary>
        /// Get the only instance of ItemList 
        /// </summary>
        /// <returns>the only instance of singleton ItemList</returns>
        public static ItemList GetItemList()
        {
            if (instance == null)
            {
                instance = new ItemList();
            }
            return instance;
        }
        /// <summary>
        /// Add an item to the list
        /// </summary>
        /// <param name="itemToAdd"></param>
        public void Add(Item itemToAdd)
        {
            items.Add(itemToAdd);
        }

        /// <summary>
        /// Remove an item from the list
        /// </summary>
        /// <param name="itemToRemove"></param>
        /// <returns>whether the item was removed successfully as a bool</returns>
        public bool Remove(Item itemToRemove)
        {
            bool success = false; ;
            success = items.Remove(itemToRemove);
            return success;
        }
        /// <summary>
        /// Method to allow the removal of certain items
        /// </summary>
        /// <param name="selectedItems">A List of Items to be removed</param>
        /// <returns>the count of items in the list</returns>
        public int RemoveItems(List<Item> selectedItems)
        {
            int removed = 0;
            //where items are marked removed, remove from the list, use Linq statement

            return removed;
        }

        /// <summary>
        /// Removes all the items that are marked Removed using linq
        /// </summary>
        /// <returns>an int count of the number of items removed</returns>
        public int RemoveItems()
        {
            int itemsRemoved = 0;
            items.RemoveAll(i => i.Removed == true);
            return itemsRemoved;
        }
        /// <summary>
        /// Clear out all the items in the instance of the list
        /// </summary>
        public void Clear()
        {
            items.Clear();
        }
        /// <summary>
        /// Converts IEnumerable of type T to an ObservableCollection of type T
        /// </summary>
        /// <typeparam name="T">object Type to convert</typeparam>
        /// <param name="enumerableListItems">collection of objects type T</param>
        /// <returns>an observable collection with items passed in, if null returns a new ObservableCollection</returns>
        public static ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> enumerableListItems)
        {
            ObservableCollection<T> observableCollection = new ObservableCollection<T>();
            if (enumerableListItems != null)
            {

                foreach (T item in enumerableListItems)
                {
                    observableCollection.Add(item);
                }

            }
            else
            {
                observableCollection = new ObservableCollection<T>();
            }
            return observableCollection;
        }
    }
}
